<!DOCTYPE html>
<html>

<head>

    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">


    <title>FooTable</title>

    <link rel="shortcut icon" href="favicon.ico"> <link href="css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
    <link href="css/font-awesome.css?v=4.4.0" rel="stylesheet">
    <link href="css/plugins/footable/footable.core.css" rel="stylesheet">

    <link href="css/animate.css" rel="stylesheet">
    <link href="css/style.css?v=4.1.0" rel="stylesheet">

</head>

<body class="gray-bg">
    <div class="wrapper wrapper-content animated fadeInRight">
		<div class="row">
			<div class="col-sm-12">
				<div class="ibox float-e-margins">
					<div class="ibox-title">
						<h5>单位管理</h5>
					</div>
					<div class="ibox-content">
						<div class="col-sm-12">
							<div class="form-horizontal">
								<!--p>新增行政区域</p-->
								<div class="form-group">
									<div class="col-sm-4">
										<input id="form-regionName" placeholder="行政区域名称" class="form-control">
									</div>
									<div class="col-sm-4">
										<button id="search-submit" class="btn btn-primary">查询</button>
									</div>
								</div>
							</div>
						</div>
						<table id="companyTable" class="table">
							<thead>
							<tr>
								<th>单位id</th>
								<th>单位名称</th>
								<th>单位代码</th>
								<th>所属级别</th>
								<th>所属行政区编号</th>
								<th>所属行政区</th>
								<th>上级单位代码</th>
								<th>操作</th>
								<th></th>
							</tr>
							</thead>
							<tbody>
							</tbody>
							<tfoot>
							</tfoot>
						</table>
						<div>
							<button id="prevButton" type="button" class="btn btn-primary">上一页</button>
							<button id="nextButton" type="button" class="btn btn-primary">下一页</button>
						</div>
					</div>
					<div id="modify-form" class="ibox float-e-margins">
						<div class="ibox-title">
							<h5>修改单元</h5>
						</div>
						
						<div class="ibox-content">
							<form class="form-horizontal">
								<!--p>新增行政区域</p-->
								<div class="form-group">
									<label class="col-sm-3 control-label">单位id：</label>
									<div class="col-sm-4">
										<input id="modify-companyId" readonly="readonly" class="form-control">
									</div>
								</div>
								<div class="form-group">
									<label class="col-sm-3 control-label">单位代码：</label>
									<div class="col-sm-4">
										<input id="modify-companyCode" placeholder="单位代码" class="form-control">
									</div>
								</div>
								<div class="form-group">
									<label class="col-sm-3 control-label">单位名称：</label>
									<div class="col-sm-4">
										<input id="modify-companyName" placeholder="单位名称" class="form-control">
									</div>
								</div>
								<div class="form-group">
									<label class="col-sm-3 control-label">所属级别：</label>
                                    <div class="col-sm-2">
										<div class="radio">
											<label>
												<input type="radio" checked="true" value="0" id="modify_optionsRadios1" name="optionsRadios">省级</label>
										</div>
										<div class="radio">
											<label>
												<input type="radio" value="1" id="modify_optionsRadios2" name="optionsRadios">市级</label>
										</div>
										<div class="radio">
											<label>
												<input type="radio" value="2" id="modify_optionsRadios3" name="optionsRadios">县级</label>
										</div>
                                    </div>
								</div>
								<div class="form-group">
									<label class="col-sm-3 control-label">所属行政区域编号</label>
									<div class="col-sm-4">
										<input id="modify-regionId" placeholder="所属行政区域编号" class="form-control">
									</div>
								</div>
								<div class="form-group">
									<label class="col-sm-3 control-label">上级单位代码：</label>
									<div class="col-sm-4">
										<input id="modify-pcompanyCode" placeholder="上级单位代码" class="form-control">
									</div>
								</div>
								<div class="form-group">
									<div class="col-sm-offset-3 col-sm-4">
										<button id="modify-submit" class="btn btn-primary">确认修改</button>
									</div>
								</div>
							</form>
						</div>
					</div>
					
					<div class="ibox float-e-margins">
						<div class="ibox-title">
							<h5>新增单元</h5>
							<div class="ibox-content">
									<button id="addButton" type="button" class="btn btn-primary">新增</button>
							</div>
						</div>
						
						<div id="add-form" class="ibox-content">
							<form class="form-horizontal">
								<!--p>新增行政区域</p-->
								<div class="form-group">
									<label class="col-sm-3 control-label">单位代码：</label>
									<div class="col-sm-4">
										<input id="form-companyCode" placeholder="单位代码" class="form-control">
									</div>
								</div>
								<div class="form-group">
									<label class="col-sm-3 control-label">单位名称：</label>
									<div class="col-sm-4">
										<input id="form-companyName" placeholder="单位名称" class="form-control">
									</div>
								</div>
								<div class="form-group">
									<label class="col-sm-3 control-label">所属级别：</label>
                                    <div class="col-sm-2">
										<div class="radio">
											<label>
												<input type="radio" checked="true" value="0" id="optionsRadios1" name="optionsRadios">省级</label>
										</div>
										<div class="radio">
											<label>
												<input type="radio" value="1" id="optionsRadios2" name="optionsRadios">市级</label>
										</div>
										<div class="radio">
											<label>
												<input type="radio" value="2" id="optionsRadios3" name="optionsRadios">县级</label>
										</div>
                                    </div>
								</div>
								<div class="form-group">
									<label class="col-sm-3 control-label">所属行政区域编号：</label>
									<div class="col-sm-4">
										<input id="form-regionCode" placeholder="所属行政区域编号" class="form-control">
									</div>
								</div>
								<div class="form-group">
									<label class="col-sm-3 control-label">上级单位代码：</label>
									<div class="col-sm-4">
										<input id="form-pcompanyCode" placeholder="上级单位代码" class="form-control">
									</div>
								</div>
								<div class="form-group">
									<div class="col-sm-offset-3 col-sm-4">
										<button id="form-submit" class="btn btn-primary">确认新增</button>
									</div>
								</div>
							</form>
						</div>
					</div>
				</div>
			</div>
		</div>
	</div>
	
    <!-- 全局js -->
    <script src="js/jquery.min.js?v=2.1.4"></script>
    <script src="js/bootstrap.min.js?v=3.3.6"></script>
    <script src="js/plugins/footable/footable.all.min.js"></script>
    <!-- 自定义js -->
    <!--script src="js/content.js?v=1.0.0"></script-->
	<script src="js/service/service.js"></script>
    <script>
		let currName;
		let currPage = 1;
		let PageSize = 5;
        $(document).ready(function() {
			$("div#add-form").hide()
			$('#nextButton').hide()
			$('#prevButton').hide()
			$('#modify-form').hide()
        });
		
		$('#search-submit').click(function(){
			currName = $('#form-regionName').val()
			if(currName != ""){
				$("#companyTable tbody").html("")
				service.getCompanyBypage(currPage, PageSize, currName, fillRegion, console.log);
			}
		});
		
		$('.footable').footable();
		function fillRegion(obj){
			var totalpage = JSON.parse(obj)["totalpage"]
			if(currPage == totalpage || totalpage == 0){
				$('#nextButton').hide()
			}else{
				$('#nextButton').show()
			}
			
			if(currPage == 1 || totalpage == 0){
				$('#prevButton').hide()
			}else{
				$('#prevButton').show()
			}
		
			var objValue = JSON.parse(obj)["data"]
			var levelArray = ["省级", "市级", "县级"]
			var table = document.getElementsByTagName("tbody")[0];
			for(var ele in objValue){
				var tr = table.insertRow(table.rows.length);
				tr.insertCell(0).innerHTML = objValue[ele]["id"]
				tr.insertCell(1).innerHTML = objValue[ele]["companyName"]
				tr.insertCell(2).innerHTML = objValue[ele]["companyCode"]
				var levelCode = objValue[ele]["levelCode"]
				if(levelCode == 0 || levelCode == 1 || levelCode == 2){
					tr.insertCell(3).innerHTML = levelArray[objValue[ele]["levelCode"]]
				}else{
					tr.insertCell(3).innerHTML = objValue[ele]["levelCode"]
				}
				tr.insertCell(4).innerHTML = objValue[ele]["region"]["id"]
				tr.insertCell(5).innerHTML = objValue[ele]["region"]["regionName"]
				tr.insertCell(6).innerHTML = objValue[ele]["pcompanyCode"]
				tr.insertCell(7).innerHTML = '<a onclick="modify(this,' + objValue[ele]["id"] +')"><i class="fa fa-wrench"></i> 修改</a>';
				tr.insertCell(8).innerHTML = '<a onclick="remove(this,' + objValue[ele]["id"] +')"><i class="fa fa-wrench"></i> 删除</a>';
			}			
		}
		
		function help(obj){
			console.log(obj)
			alert('操作成功')
		}
		
		function remove(cell,id){
			service.deleteCompany(id, help, console.log)
			$('#modify-form').hidden()
			var tr=cell.parentNode.parentNode;
			var table=document.getElementsByTagName("table")[0]
			table.deleteRow(tr.rowIndex);
		}
		
		function modify(cell, id){
			$('#modify-form').show()
			var tr = cell.parentNode.parentNode;
			$('#modify-companyId').attr('value',tr.cells[0].innerText)
			$('#modify-companyCode').attr('value',tr.cells[1].innerText)
			$('#modify-companyName').attr('value',tr.cells[2].innerText)
			$('#modify-regionId').attr('value',tr.cells[4].innerText)
			$('#modify-pcompanyCode').attr('value',tr.cells[6].innerText)
			
			var levelArray = ["省级", "市级", "县级"]
			var level = tr.cells[3].innerText
			if(level == levelArray[0]){
				$('#modify_optionsRadios1').attr('checked', true)
			}else if(level == levelArray[1]){
				$('#modify_optionsRadios2').attr('checked', true)
			}else if(level == levelArray[2]){
				$('#modify_optionsRadios3').attr('checked', true)
			}
		}
				
		$('#nextButton').click(function(){
			$("#companyTable tbody").html("")
			currPage = currPage + 1
			service.getCompanyBypage(currPage, PageSize, currName, fillRegion, console.log);
		});
		
		$('#prevButton').click(function(){
			$("#companyTable tbody").html("")
			currPage = currPage - 1
			service.getCompanyBypage(currPage, PageSize, currName, fillRegion, console.log);
		});
		
		$('#addButton').click(function(){
			if($('div#add-form').is(':hidden')){
				$('div#add-form').show()
				$('#addButton').text("取消新增")
			}else{
				$('div#add-form').hide()
				$('#addButton').text("新增")
			}
		});
		
		$('#modify-submit').click(function(){
			var id, companyCode, companyName, companylevel, regionCode, pcompanyCode
			
			id = $('#modify-companyId').val()
			companyCode = $('#modify-companyCode').val()
			companyName = $('#modify-companyName').val()
			regionCode = $('#modify-regionId').val()
			pcompanyCode = $('#modify-pcompanyCode').val()
			
			if($('#modify_optionsRadios1').attr("checked")){
				companylevel = $('#optionsRadios1').val()
			}else if($('#modify_optionsRadios2').attr("checked")){
				companylevel = $('#optionsRadios2').val()
			}else if($('#modify_optionsRadios3').attr("checked")){
				companylevel = $('#optionsRadios3').val()
			}
			
			if(companyCode == "" || companyName == "" || companylevel == "" || regionCode == "" || pcompanyCode == ""){
				alert("请完整填写内容")
			}else{
				service.patchCompany(id, companyCode, companyName, companylevel, pcompanyCode, regionCode, help, console.log)
			}
		});
		
		$('#form-submit').click(function(){
			var companyCode, companyName, companylevel, regionCode, pcompanyCode
			companyCode = $('#form-companyCode').val()
			companyName = $('#form-companyName').val()
			regionCode = $('#form-regionCode').val()
			pcompanyCode = $('#form-pcompanyCode').val()
			
			if($('#optionsRadios1').attr("checked")){
				companylevel = $('#optionsRadios1').val()
			}else if($('#optionsRadios2').attr("checked")){
				companylevel = $('#optionsRadios2').val()
			}else if($('#optionsRadios3').attr("checked")){
				companylevel = $('#optionsRadios3').val()
			}
			
			if(companyCode == "" || companyName == "" || companylevel == "" || regionCode == "" || pcompanyCode == ""){
				alert("请完整填写内容")
			}else{
				service.addCompany(companyCode, companyName, companylevel, pcompanyCode, regionCode, help, console.log)
			}
		});
		
    </script>

    <script type="text/javascript" src="http://tajs.qq.com/stats?sId=9051096" charset="UTF-8"></script>
    <!--统计代码，可删除-->

</body>

</html>
